package com.sugon.controller.analyse;

import cn.hutool.core.collection.CollectionUtil;
import com.sugon.modules.analyse.service.IMouldAnalyseService;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 模具分析Controller
 * @author: YuXD
 * @create: 2020-05-10 17:08
 **/
@RestController
@RequestMapping("mould/stat")
public class MouldAnalyseController {

    @Autowired
    private IMouldAnalyseService mouldAnalyseService;

    /**
     * 模具故障率统计
     * 横坐标：近六个月
     *
     * @param workshopId 车间ID
     * @param moldTypeId 模具类型ID
     * @return
     */
    @RequestMapping("/faultRate")
    public R moldFaultRateStat(@RequestParam String workshopId, @RequestParam String moldTypeId) {
        return R.ok().put("statResult", mouldAnalyseService.statMoldFaultRate(workshopId, moldTypeId));
    }

    /**
     * 模具大屏 维修统计
     * 包括本日报修数、本日完成数、报修列表
     *
     * @return
     */
    @RequestMapping("/repair")
    public R moldRepairStat(@RequestParam String workshopId) {
        return R.ok().put("mouldStatusStat", mouldAnalyseService.moldRepairStat(workshopId));
    }

    /**
     * 模具维修状态统计
     *
     * @param workshopId
     * @return
     */
    @RequestMapping("/status")
    public R statUserAvailability(@RequestParam(required = false) String workshopId) {
        // 获取所有模具状态信息
        return R.ok().put("mouldStatusStat", mouldAnalyseService.statMouldStatus(workshopId));
    }

    /**
     * 模具故障次数TOP统计
     *
     * @param params 月份和次数参数
     * @return
     */
    @RequestMapping("/faultTimesTopStat")
    public R faultClassifyTop(@RequestParam Map<String, Object> params) {
        try {
            return R.ok().put("faultTimesTopStatData", mouldAnalyseService.faultTimesTopStat(params));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 模具分类 近6个月份 实际单台成本变化趋势
     *
     * @param workshopId
     * @return
     */
    @RequestMapping("/moldTypeMonthlyCostChangeTrend")
    public R moldTypeMonthlyCostChangeTrend(@RequestParam(required = false) String workshopId) {
        // 获取所有模具状态信息
        return R.ok().put("moldTypeMonthlyCostChangeData", mouldAnalyseService.statMoldTypeMonthlyCostChange(workshopId));
    }

    /**
     * 模具和设备故障对比
     *
     * @return
     */
    @RequestMapping("/mold2DeviceFaultContract")
    public R mold2DeviceFaultContract() {

        List<String> dimensions = CollectionUtil.newArrayList("Name", "Type", "Near Bridge");
        List<List<Object>> data = CollectionUtil.newArrayList(CollectionUtil.newArrayList("模具异常", "E", true), CollectionUtil.newArrayList("设备异常", "E", true), CollectionUtil.newArrayList("正常时间", "N", true));

        Map<String, Object> parkingApron = new HashMap<>();
        parkingApron.put("dimensions", dimensions);
        parkingApron.put("data", data);


        Map<String, Object> flight = new HashMap<>();
        List<String> flightDimensions = CollectionUtil.newArrayList("索引", "开始时间", "结束时间", "模具号", "VIP", "Arrival Company", "Departure Company", "Arrival Line", "Departure Line", "Report Time");
        List<List<Object>> flightData = CollectionUtil.newArrayList(CollectionUtil.newArrayList(2,
                1635731413000L,
                1635904213000L,
                "Z2-GG-NE15-5",
                false,
                "QW",
                "QW",
                "XGF-HAC",
                "HAC-CFA",
                1635904213000L), CollectionUtil.newArrayList(2,
                1635994213000L,
                1636001413000L,
                "Z2-YZ-NE15-3",
                false,
                "AS",
                "AS",
                "MGT-HAC",
                "HAC-YPP",
                1636001413000L), CollectionUtil.newArrayList(1,
                1636001713000L,
                1636012513000L,
                "Z2-GG-NE15-2",
                false,
                "AS",
                "AS",
                "KMS-HAC",
                "HAC-YPP",
                1636012513000L), CollectionUtil.newArrayList(1,
                1636098913000L,
                1636106113000L,
                "Z2-GG-NE14-3",
                true,
                "WA",
                "WA",
                "RMX-DWH-HAC",
                "HAC-XTL",
                1636106113000L), CollectionUtil.newArrayList(0,
                1636142113000L,
                1636149313000L,
                "Y4686",
                true,
                "WA",
                "WA",
                "RMX-DWH-HAC",
                "HAC-XTL",
                1636149313000L), CollectionUtil.newArrayList(2,
                1636156513000L,
                1636163713000L,
                "Y4897",
                true,
                "AS",
                "AS",
                "MPT-HAC",
                "HAC-KVP",
                1636163713000L));
        flight.put("dimensions", flightDimensions);
        flight.put("data", flightData);

        Map<String, Object> contrastResult = new HashMap<>();
        contrastResult.put("parkingApron", parkingApron);
        contrastResult.put("flight", flight);
        // 获取所有模具状态信息
        return R.ok().put("contrastResult", contrastResult);
    }


    /**
     * 模具故障处理时间段统计分析
     * <p>
     * 从故障时间和故障处理时间两个维度分析
     */
    @RequestMapping("/faultDurationAnalyse")
    public R faultDurationAnalyse(@RequestBody Map<String, Object> param) {
        return R.ok().put("result", mouldAnalyseService.getFaultDurationStat(param));
    }


    /**
     * 模具故障分类次数统计，主要统计某个车间某月份下某模具分类的故障次数
     *
     * @param params 包含月份(month)、模具分类(mouldTypeId)或所属车间(dept)
     * @return
     */
    @RequestMapping("/faultClassifyTimes")
    public R faultClassify(@RequestParam Map<String, Object> params) {
        try {
            return R.ok().put("faultClassifyTimesStatData", mouldAnalyseService.faultClassifyTimesStat(params));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 模具故障分类次数统计，主要统计某个车间某月份下某模具分类的故障次数
     *
     * @param params 包含年份(year)、模具分类(mouldTypeId)或所属车间(dept)
     * @return
     */
    @RequestMapping("/cost")
    public R moldCostStat(@RequestBody Map<String, Object> params) {
        try {
            return R.ok().put("moldCostStatData", mouldAnalyseService.doMoldCostStat(params));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

}
